package com.bicycle.rentalsystem.dao.impl.admin;

import com.bicycle.rentalsystem.dao.admin.BicycleDao;
import com.bicycle.rentalsystem.pojo.admin.Bicycle;

import java.util.List;

/**
 *自行车数据层实现类
 * 用于实现自行车数据层接口
 *
 */
public class BicycleDaoImpl extends BaseDao implements BicycleDao {
    @Override
    public List<Bicycle> searchBicycles() {
        return null;
    }

    @Override
    public int updateBicycle(String id, String bicycleName, String describe) {
        return 0;
    }


    @Override
    public Integer queryForPageTotalCount() {
        String sql="select count(*) from bicycle";
        Object o = queryForSingleValue(sql);
        Integer s = Integer.valueOf(o.toString());
        return s;
    }

    @Override
    public List<Bicycle> queryForPageItems(Integer begin, Integer pageSize) {
        String sql="SELECT bid,number,`type`,bname,rent,`status`,ssid,station_name stationName\n" +
                "FROM bicycle,station \n" +
                "WHERE ssid=sid\n" +
                "LIMIT ?,?;\n";
        List<Bicycle> list = queryForList(Bicycle.class, sql, begin, pageSize);
        return list;
    }

    @Override
    public List<Bicycle> queryBicycles() {
        String sql="SELECT bid,number,`type`,bname,rent,`status`,ssid,station_name stationName\n" +
                "FROM bicycle,station \n" +
                "WHERE ssid=sid";
        return queryForList(Bicycle.class,sql);
    }


    @Override
    public void saveBike(Bicycle bike) {
        Integer ssid = queryStationId(bike.getStationName());
        String sql="INSERT INTO bicycle(`number`,`type`,`bname`,`rent`,`status`,`ssid`)\n" +
                "VALUES(?,?,?,?,?,?)";
        update(sql,bike.getNumber(),bike.getType(),bike.getBname(),bike.getRent(),bike.getStatus(),ssid);
    }

    @Override
    public Integer queryStationId(String stationName) {
        String sql="SELECT sid FROM station WHERE station_name=?";
        Object o = queryForSingleValue(sql, stationName);
        String s = o.toString();
        Integer a= Integer.valueOf(s);
        return a;
    }

    @Override
    public void deleteBike(Integer bid) {
        String sql="delete from bicycle where bid=?";
        update(sql, bid);
    }

    @Override
    public Bicycle getBikeById(Integer bid) {
        String sql="SELECT bid,number,`type`,bname,rent,`status`,ssid,station_name stationName\n" +
                "FROM bicycle ,station WHERE bid=?";
        Bicycle bicycle = queryForOne(Bicycle.class, sql, bid);
        return bicycle;
    }

    @Override
    public void updateBike(Bicycle bike) {
        Integer ssid = queryStationId(bike.getStationName());
        String sql="UPDATE bicycle SET `number`=?,`type`=?,bname=?,rent=?,`status`=?,ssid=? WHERE bid=?";
        update(sql,bike.getNumber(),bike.getType(),bike.getBname(),bike.getRent(),bike.getStatus(),ssid,bike.getBid());
    }

    @Override
    public int queryStatus() {
        String sql="select count(*) from bicycle where status='已租赁'";
        Object o = queryForSingleValue(sql);
        String s = o.toString();
        int a= Integer.parseInt(s);
        return a;
    }
}
